package com.xxx.car.web.api;

import com.xxx.car.entity.vo.request.ReqRedpackageDetail;
import com.xxx.car.entity.vo.request.ReqRedpackageWithdraw;
import com.xxx.car.entity.vo.request.ReqUserWalletGet;
import com.xxx.car.entity.vo.response.RespRedPackageDetail;
import com.xxx.car.entity.vo.response.RespUserWallet;
import com.xxx.car.service.CouponService;
import com.xxx.car.service.UserWalletService;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by xuwenjie on 2017/6/16.
 */
@Controller
@RequestMapping("/api/wallet")
public class ApiUserWalletController {

    @Autowired
    private UserWalletService userWalletService;

    @Autowired
    private CouponService couponService;

    @RequestMapping("/get")
    @ResponseBody
    public APIResponse get(@Valid @ModelAttribute ReqUserWalletGet reqUserWalletGet, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        RespUserWallet wallet = userWalletService.getByUser(reqUserWalletGet.getUserId());
        Integer couponCount = couponService.queryUserCouponCount(reqUserWalletGet.getUserId());
        wallet.setCouponCount(couponCount);
        return APIResponse.returnSuccess(wallet);
    }

    @RequestMapping("/redpackage/detail")
    @ResponseBody
    public APIResponse redpackageDetail(@Valid @ModelAttribute ReqRedpackageDetail reqRedpackageDetail, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        List<RespRedPackageDetail> detail = userWalletService.listRedPackageDetail(reqRedpackageDetail);
        return APIResponse.returnSuccess(detail);
    }

    @RequestMapping("/redpackage/withdraw")
    @ResponseBody
    public APIResponse redpackageWithdraw(@Valid @ModelAttribute ReqRedpackageWithdraw reqRedpackageWithdraw, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        userWalletService.redpackageWithdraw(reqRedpackageWithdraw);
        return APIResponse.returnSuccess();
    }


}
